package cn.gp.chatclient.ui.frame;

import cn.gp.chatclient.model.UserVo;
import cn.gp.chatclient.service.UserService;
import cn.gp.chatclient.ui.panel.*;

import javax.swing.*;
import java.awt.*;

/**
 * @ClassName MainFrame
 * @Description TODO
 * @Author He Guoping
 * @Date 2025/6/17 9:09
 * @Version JDK17
 */
public class MainFrame extends JFrame {
    private final UserService userService;
    private final UserVo currentUser;
    private final CardLayout cardLayout;
    private final JPanel contentPanel;
    private final ContactListPanel contactListPanel;
    private final ChatPanel chatPanel;
    private final ProfilePanel profilePanel;
    private final FriendRequestPanel friendRequestPanel;

    public MainFrame(UserService userService, UserVo currentUser) {
        this.userService = userService;
        this.currentUser = currentUser;
        // 显式设置当前用户ID，确保后续API调用不会出现未登录
        this.userService.setCurrentUserId(currentUser.getId());

        setTitle(currentUser.getUsername() != null ? currentUser.getUsername() : "GP");
        setSize(1000, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 初始化CardLayout和内容面板
        cardLayout = new CardLayout();
        contentPanel = new JPanel(cardLayout);

        // 创建各个功能面板
        contactListPanel = new ContactListPanel(currentUser, this::switchToChat);
        // 设置userService
        contactListPanel.setUserService(userService);
        chatPanel = new ChatPanel(currentUser);
        profilePanel = new ProfilePanel(currentUser);
        friendRequestPanel = new FriendRequestPanel(userService, currentUser);

        // 添加面板到内容面板
        contentPanel.add(contactListPanel, "contacts");
        contentPanel.add(chatPanel, "chat");
        contentPanel.add(profilePanel, "profile");
        contentPanel.add(friendRequestPanel, "friendRequests");

        // 创建左侧导航栏
        LeftPanel leftPanel = new LeftPanel(currentUser);
        leftPanel.setNavigationListener(panelName -> {
            switch (panelName) {
                case "contacts" -> {
                    cardLayout.show(contentPanel, "contacts");
                    contactListPanel.refreshContacts();
                }
                case "chat" -> cardLayout.show(contentPanel, "chat");
                case "profile" -> {
                    cardLayout.show(contentPanel, "profile");
                    profilePanel.refreshProfile();
                }
                case "friendRequests" -> {
                    cardLayout.show(contentPanel, "friendRequests");
                    friendRequestPanel.refreshRequests();
                }
            }
        });

        // 添加面板到主面板
        mainPanel.add(leftPanel, BorderLayout.WEST);
        mainPanel.add(contentPanel, BorderLayout.CENTER);

        add(mainPanel);

        // 默认显示联系人面板
        cardLayout.show(contentPanel, "contacts");
    }

    public void switchToChat(UserVo.FriendVo friend) {
        // 将FriendVo转换为UserVo
        UserVo friendUser = new UserVo(
            friend.getId(),
            friend.getUsername(),
            friend.getNickname(),
            friend.getAvatar(),
            friend.getStatus(),
            null  // friends列表设为null，因为不需要
        );
        chatPanel.setCurrentContact(friendUser);
        cardLayout.show(contentPanel, "chat");
    }
}
